package com.caishi.lkx.user.ienum.type;
import com.baomidou.mybatisplus.annotation.EnumValue;
import lombok.Getter;

import java.util.Arrays;

public enum HaveType {
    // 账号拥有平台
    vers7("vers7所有", 1),
    ykt("云课堂所有", 2),
    kaoyan("考研所有", 4);

    @Getter
    final
    String desc;

    @Getter
    @EnumValue
    final
    Integer value;

    HaveType(String desc, Integer value) {
        this.desc = desc;
        this.value = value;
    }
    public static HaveType int2Obj(int value) {
        return Arrays.stream(HaveType.values())
                .filter(v -> v.value == value).findFirst().orElse(null);
    }

    public static int array2Int(HaveType[] haveType) {
        int result = 0;
        for (HaveType l : haveType) {
            result |= l.getValue();
        }
        return result;
    }

    public static HaveType[] int2Array(int value) {
        return Arrays.stream(HaveType.values())
                .filter(v -> (v.getValue() & value) == v.getValue()).toArray(HaveType[]::new);
    }

    public static boolean intHaveType(int value, HaveType level) {
        return (value & level.value) == level.value;
    }

    public static boolean intAllHaveType(int value, HaveType[] levels) {
        return Arrays.stream(levels).allMatch(v -> (v.getValue() & value) == v.getValue());
    }

    public static boolean intAnyHave(int value, HaveType[] levels) {
        return Arrays.stream(levels).anyMatch(v -> (v.getValue() & value) == v.getValue());
    }
}

